﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using blogDemo.Models;
using System.Web.Security;  //Admin
using PagedList;

namespace blogDemo.Controllers
{
    [Authorize] //验证是否登录
    public class adminController : Controller
    {
        private blogDemoDbContext db = new blogDemoDbContext();


        //
        // GET: /admin/

        public ActionResult Index(int? page)
        {
            int pageNumber = page ?? 1;
            int pageSize = 5;
            //Skip之前必须orderby
            var artics = (from a in db.artics
                          orderby a.ID descending
                          select a);

            ViewBag.user = User.Identity.Name;

            return View(artics.ToPagedList(pageNumber, pageSize));
        }

        //
        // GET: /admin/Details/5

        public ActionResult Details(int id = 0)
        {
            artic artic = db.artics.Find(id);
            if (artic == null)
            {
                return HttpNotFound();
            }

            ViewBag.user = User.Identity.Name;

            return View(artic);
        }

        //
        // GET: /admin/Create

        public ActionResult Create()
        {
            ViewBag.user = User.Identity.Name;
            return View();
        }

        //
        // POST: /admin/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        [ValidateInput(false)]
        public ActionResult Create(artic artic)
        {
            artic.author = User.Identity.Name;
            artic.date = DateTime.Now;
            artic.views = 0;
            if (ModelState.IsValid)
            {
                db.artics.Add(artic);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(artic);
        }

        //
        // GET: /admin/Edit/5

        public ActionResult Edit(int id = 0)
        {
            artic artic = db.artics.Find(id);
            if (artic == null)
            {
                return HttpNotFound();
            }

            ViewBag.user = User.Identity.Name;

            return View(artic);
        }

        //
        // POST: /admin/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        [ValidateInput(false)]
        public ActionResult Edit(artic artic)
        {
            if (ModelState.IsValid)
            {
                db.Entry(artic).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(artic);
        }

        //
        // GET: /admin/Delete/5

        public ActionResult Delete(int id = 0)
        {
            artic artic = db.artics.Find(id);
            if (artic == null)
            {
                return HttpNotFound();
            }

            ViewBag.user = User.Identity.Name;

            return View(artic);
        }

        //
        // POST: /admin/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            artic artic = db.artics.Find(id);
            db.artics.Remove(artic);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        //
        //GET:  /Admin/Login

        [AllowAnonymous]
        public ActionResult Login()
        {
            return View();
        }

        //
        //POST: /Admin/Login

        [HttpPost]
        [AllowAnonymous]
        public ActionResult Login(user user)
        {
            if (ModelState.IsValid)
            {
                int rst = db.users.Count(a => a.username == user.username && a.password == user.password);
                ViewBag.rst = rst;
                if (rst == 1)
                {
                    FormsAuthentication.SetAuthCookie(user.username, false);
                    return RedirectToAction("Index");

                }
            }
            return View();
        }

        [AllowAnonymous]
        public ActionResult Regist()
        {
            return View();
        }

        //
        //POST: /Admin/Regist

        [HttpPost]
        [AllowAnonymous]
        public ActionResult Regist(user user)
        {
            string regable = System.Configuration.ConfigurationManager.AppSettings["regable"];
            if (regable == "true")
            {
                if (ModelState.IsValid)
                {
                    int rst = db.users.Count(a => a.username == user.username);
                    if (rst == 0)
                    {
                        db.users.Add(user);
                        db.SaveChanges();
                    }
                    else
                    {
                        ViewBag.errmsg = "用户名已存在";
                        return View();
                    }

                }
            }
            else
            {
                ViewBag.errmsg = "现在禁止注册";
                return View();
            }

            return RedirectToAction("Login");
        }

        public ActionResult Logout()
        {
            FormsAuthentication.SignOut();
            return RedirectToAction("login");
        }

        //
        // 用户操作
        //
        // GET: /users/

        public ActionResult userIndex()
        {
            return View(db.users.ToList());
        }

        //
        // GET: /users/Create

        public ActionResult userCreate()
        {
            return View();
        }

        //
        // POST: /users/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult userCreate(user user)
        {
            user.level = 3;

            if (ModelState.IsValid)
            {
                db.users.Add(user);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(user);
        }

        //
        // GET: /users/Edit/5

        public ActionResult userEdit(int id = 0)
        {
            user user = db.users.Find(id);
            if (user == null)
            {
                return HttpNotFound();
            }
            return View(user);
        }

        //
        // POST: /users/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult userEdit(user user)
        {
            if (ModelState.IsValid)
            {
                db.Entry(user).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("userIndex");
            }
            return View(user);
        }

        //
        // GET: /users/Delete/5

        public ActionResult userDelete(int id = 0)
        {
            user user = db.users.Find(id);
            if (user == null)
            {
                return HttpNotFound();
            }
            else
            {
                db.users.Remove(user);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}